<?php

use yii\helpers\Html;
use mdm\admin\components\Helper;
use common\components\TGridView;
use yii\helpers\Url;
use yii\bootstrap\ActiveForm;
use common\helpers\TStringHelper;

/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('common', 'news');
$this->params['breadcrumbs'][] = $this->title;
?>
<?=Html::jsFile('/static/common/js/jquery.imagePreview.js')?>
<div class="news-index">
    <?php echo $this->render('_search', ['params' => $params, 'category' => $category]); ?>
    <?php
    $gridColumns = [
        [
            'name' => 'selectedIds',
            'class' => 'kartik\grid\CheckboxColumn',
            'checkboxOptions' => function($model, $key, $index, $column) {
                return [
                    'value' => $model->id,
                ];
            }
        ],
        [
            'class' => 'kartik\grid\SerialColumn',
            'header' => Yii::t('common','serial_number'),
        ],
        [
            'attribute' => 'title',
            'header'=> Yii::t('common', 'title'),
        ],
        [
            'header'=> Yii::t('common', 'category'),
            'value' => function($model) {
                return !empty($model->category_id) ? \common\models\NewsCategory::getNewsCategoryName($model->category_id) : '--';
            },
            'width' => '100px',
        ],
        //'desc:raw',
        [
            'header'=> Yii::t('common', 'litpic'),
            'value' => function($model){
                return !empty($model->litpic) ? Html::img($model->litpic, ['class' => 'img-thumbnail preview', 'style' => 'max-width: 60px;', 'bimg' => $model->litpic, 'title' => $model->title]) : null;
            },
            'format' => 'raw',
        ],
        [
            'attribute' => 'views',
            'header'=> Yii::t('common', 'views'),
            'width' => '100px',
        ],
        [
            'header'=> Yii::t('common', 'status'),
            'value' => function($model){
                return \common\helpers\TStringHelper::getStatusText($model->status);
            },
            'width' => '100px',
        ],
        [
            'header'=> Yii::t('common', 'order'),
            'value' => function($model){
                return Html::textInput('orders['.$model->id.']', $model->order, ['class' => 'form-control', 'style' => 'width: 50px;']);
            },
            'format'=>'raw',
        ],
        [
            'attribute' => 'created_at',
            'value' => function($model){
                return \common\helpers\TStringHelper::ShowDate($model->created_at, 1);
            }
        ],
        [
            'class' => 'kartik\grid\ActionColumn',
            'header' => Yii::t('common', 'operation_button'),
            'template' => '{view}{updatepop}{statusButton}{deleteButton}',
            'width' => '240px',
            'buttons' => [
                'updatepop' => function ($url, $model, $key) {
                    return TStringHelper::canBeModified($model->created_by) ? Html::a('<span class="glyphicon glyphicon-pencil"></span>', Yii::$app->urlManager->createUrl(['news/update', 'id' => $model->id]), ['class' => 'EditButton', 'title' => Yii::t('common', 'edit_button')]) : '';
                },
                'statusButton' => function ($url, $model, $key) {
                    if ($model->status == \common\models\News::STATUS_FLAG_NORMAL) {
                        $title = Yii::t('common', 'change_status_stop');
                        $class = 'glyphicon glyphicon-pause';
                        $status = \common\models\News::STATUS_FLAG_STOP;
                    }
                    else
                    {
                        $title = Yii::t('common', 'change_status_start');
                        $class = 'glyphicon glyphicon-play';
                        $status = \common\models\News::STATUS_FLAG_NORMAL;
                    }
                    return TStringHelper::canBeModified($model->created_by) ? Html::a('<span class="'.$class.'"></span>', '#', ['id'=>'StatusButton', 'title'=> $title, 'onclick' => 'statusButton("' . Yii::$app->urlManager->createUrl(['news/status', 'id' => $model->id, 'status' => $status]) . '");']) : '';
                },
                'deleteButton' => function ($url, $model, $key) {
                    return TStringHelper::canBeModified($model->created_by) ? Html::a('<span class="glyphicon glyphicon-trash"></span>', '#', ['class' => 'DeleteButton', 'title' => Yii::t('common', 'delete_button'), 'onclick' => 'showDeleteModal("' . $key . '","' . Yii::$app->urlManager->createUrl(['news/delete', 'id' => $model->id]) . '");']) : '';
                },
            ],
        ],
    ];

    $contentName = Yii::t('common', 'news');

    $addbtn = '';
    if(Helper::checkRoute('create')) {
        $addbtn = Html::a(Yii::t('common', 'create_{modelClass}', ['modelClass' => Yii::t('common', 'news'),]), ['create'], ['class' => 'btn btn-default greenBtn']);
    }

    $form = ActiveForm::begin([
        'id' => 'list-form'
    ]);

    echo TGridView::widget([
        'id'=>'grid',
        'dataProvider' => $dataProvider,
        //'filterModel' => $searchModel,
        'columns' => $gridColumns,
        'panel' => [
            'type' => TGridView::TYPE_DEFAULT,
            'heading' => '<h3 class="panel-title" style="text-align: left;"><i class="glyphicon glyphicon-book"></i> ' .Yii::t('common', '{value}_record', ['value'=>$contentName]).'</h3>',
        ],
        'toolbar' => [
            ['content'=>
                $addbtn
                .' '.
                (TStringHelper::canBeModified() ? Html::button('<i class="glyphicon glyphicon-refresh"></i> '.Yii::t('common', 'batch_update_button'),['title'=>Yii::t('common', 'batch_update_button'), 'class'=>'btn btn-default blueBtn', 'onclick' => 'batchUpdateOrderBtn("'.Yii::$app->urlManager->createUrl(['news/batch-update-order']).'", "list-form")']) : '')
                .' '.
                (TStringHelper::canBeModified() ? Html::button('<i class="glyphicon glyphicon-minus"></i> '.Yii::t('common', 'batch_delete_button'),['title'=>Yii::t('common', 'batch_delete_button'), 'class'=>'btn btn-default redBtn', 'onclick' => 'batchDeleteBtn("'.Yii::$app->urlManager->createUrl(['news/batch-delete']).'")']) : ''),
            ],
//            '{export}',
//            '{toggleData}'
        ],
        'pjax'=> false,
        'pjaxSettings'=>[
            'neverTimeout'=>true,
        ]
    ]);

    ActiveForm::end();
    ?>
    <div class="text-left">
        <?php
        echo \yii\widgets\LinkPager::widget([
            'id' => 'news_page',
            'pagination'=>$dataProvider->pagination,
        ]);
        ?>
    </div>
</div>

<script>
    function loadList() {
        location.reload();
    }
    $(function(){
        $(".preview").preview();
    });
</script>